package priv.lirenhe.dissertation.component.crawler_robot.url_manager.url_ini;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

public class ParamProperties {
	private String fileResourceName;
	private String fileResourcePath;
	private Properties properties;
	
	public ParamProperties(String fileResourceName){
		this.fileResourceName = fileResourceName;
		this.properties = new Properties();
		
		try{
			this.properties.load(ParamProperties.class.getClassLoader().getResourceAsStream(this.fileResourceName));
			this.fileResourcePath = ParamProperties.class.getClassLoader().getResource(fileResourceName).getPath();
		}catch(IOException e){
			e.printStackTrace();
		}
	}
	
	public boolean setPropertyAndStore(String key,String value){
		boolean flag = false;
		try{
			this.properties.setProperty(key, value);
			store();
			flag = true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return flag;
	}
	
	public boolean setAllPropertyAndStore(Map<String,String> map){
		boolean flag = false;
		try{
			for(Map.Entry<String, String> entry:map.entrySet()){
				this.properties.setProperty(entry.getKey(), entry.getValue());
			}
			
			store();
			flag = true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return flag;
	}
	
	public String getPropertyByKey(String key){
		String value = null;
		if(this.properties.containsKey(key)){
			value = this.properties.getProperty(key);
		}
		return value;
	}
	
	public String getPropertyByKeyAndDeleteAndStore(String key){
		String value = null;
		if(this.properties.containsKey(key)){
			value = this.properties.getProperty(key);
			this.properties.remove(key,value);
			store();
		}
		return value;
	}
	
	public String getNextPropertyAndDeleteAndStore(){
		String value = null;
		Entry<Object, Object> entry = null;
		if(!this.properties.isEmpty()){
			entry = this.properties.entrySet().iterator().next();
			value = (String)entry.getValue();
			this.properties.remove(entry.getKey(),value);
			store();
		}
		return value;
	}
		
	public Map<String,String> getAllPropertyInMap(){
		Map<String,String> map = null;
		Iterator<Entry<Object, Object>> iterator = null;
		Entry<Object, Object> entry = null;
		map = new HashMap<String,String>();
		if(!this.properties.isEmpty()){
			iterator = this.properties.entrySet().iterator();
			while(iterator.hasNext()){
				entry = iterator.next();
				map.put((String)entry.getKey(), (String)entry.getValue());
			}
		}
		return map;
	}
	
	public boolean deletePropertyByKeyAndStore(String key){
		boolean flag = false;
		try{
			if(this.properties.containsKey(key)){
				this.properties.remove(key);
				store();
			}
			flag = true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return flag;
	}
	
	public boolean deletePropertyByValueAndStore(String key,String value){
		boolean flag = false;
		try{
			if(this.properties.containsValue(value)){
				this.properties.remove(key,value);
				store();
			}
			flag = true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return flag;
	}
	
	public boolean deleteAllPropertiesAndStore(){
		boolean flag = false;
		this.properties.clear();
		store();
		flag = true;
		return flag;
	}
	
	public boolean replaceKeyOfProperty(String oldKey,String newKey){
		boolean flag = false;
		String value = null;
		value = this.properties.getProperty(oldKey);
		this.properties.remove(oldKey);
		if(value != null){
			flag = setPropertyAndStore(newKey,value);
		}
		return flag;
	}
	
	private void store(){
		File file = null;
		Writer fileWriter = null;
		try {
			file = new File(this.fileResourcePath);
			fileWriter = new FileWriter(file);
			this.properties.store(fileWriter, "commonts");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
